package com.golxen.framework.executor;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

public class ThreadPlugin {

    static ThreadPlugin instance = null;

    public static void start() {
        if (instance != null) {
            return;
        }

        instance = new ThreadPlugin();


        instance.timerExecutorService = new GameScheduledThreadPoolExecutor(corePoolSize,
                new GeneralThreadFactory("timer"));


        instance.serverCommandExecutor = Executors.newFixedThreadPool(corePoolSize * 2,
                new GeneralThreadFactory("cmd"));
    }

    public static boolean active() {
        return instance != null;
    }

    static final int cpu = Runtime.getRuntime().availableProcessors();

    static final int corePoolSize = Math.min(8, Math.max(4, cpu * 2));
    ScheduledExecutorService timerExecutorService;
    ExecutorService serverCommandExecutor;

    public static ScheduledExecutorService getTimerExecutorService() {
        return instance.timerExecutorService;
    }

    public static ExecutorService getServerCommandExecutor() {
        return instance.serverCommandExecutor;
    }

    public static int getCpu() {
        return cpu;
    }
}
